package offer;

/**
 * @Author Elephas
 * @Date 2022/2/2
 **/
@FunctionalInterface
public interface ReversePrefix {
    String reversePrefix(String s,char ch);
}
class ReversePrefixImpl1 implements ReversePrefix{
    static final int NOT_FIND = -1;
    @Override
    public String reversePrefix(String s, char ch) {
        int pos = findReversePosition(s,ch);
        if(pos == NOT_FIND){
            return s;
        }else{
            return reverseString(s,0,pos);
        }
    }
    private int findReversePosition(String s,char ch){
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == ch){
                return i;
            }
        }
        return NOT_FIND;
    }
    private String reverseString(String s,int left,int right){
        StringBuilder ans = new StringBuilder(s);
        while(left < right){
            char temp = ans.charAt(left);
            ans.setCharAt(left,ans.charAt(right));
            ans.setCharAt(right,temp);
            left++;
            right--;
        }
        return ans.toString();
    }
}

